package com.robusta.cityuate.activities;

import android.app.ProgressDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.DialogInterface.OnCancelListener;
import android.content.Intent;
import android.content.IntentFilter;
import android.graphics.Color;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import com.robusta.cityuate.R;
import com.robusta.cityuate.base.CityuateFragmentMenuActivity;
import com.robusta.cityuate.objects.Reservation;
import com.robusta.cityuate.services.CityuateBroadCastReceiver;
import com.robusta.cityuate.services.HTTPService;
import com.robusta.cityuate.utils.ConnectionUtils;
import com.robusta.cityuate.utils.TimeUtils;

public class ParkingStatusActivity extends CityuateFragmentMenuActivity {
	private static final String CHECK_OUT_TAG = ParkingStatusActivity.class
			.getName();
	// TextView tv; // textview to display the countdown
	Reservation reservation;
	private Context mContext = this;
	private TextView timerText;
	private ReservationDounCounter counter;

	CityuateBroadCastReceiver checkOutReceiver = new CityuateBroadCastReceiver() {

		@Override
		public void onSuccess() {
			reservation = null;
			setSupportProgressBarIndeterminateVisibility(false);
			counter.cancel();
			timerText.setText(R.string.checked_out);
			timerText.setTextColor(Color.GREEN);
			if (dialog != null) {
				dialog.dismiss();
				dialog = null;
			}
			
			ConnectionUtils.clearConnection(mContext, CHECK_OUT_TAG);

		}

		@Override
		public void onFailure() {
			setSupportProgressBarIndeterminateVisibility(false);
			if (dialog != null) {
				dialog.dismiss();
				dialog = null;
			}
			Toast.makeText(mContext,
					ConnectionUtils.getExtra(mContext, CHECK_OUT_TAG),
					Toast.LENGTH_LONG).show();
			ConnectionUtils.clearConnection(mContext, CHECK_OUT_TAG);
		}

		@Override
		public void onCancel() {
			setSupportProgressBarIndeterminateVisibility(false);
			if (dialog != null) {
				dialog.dismiss();
				dialog = null;
			}
			ConnectionUtils.clearConnection(mContext, CHECK_OUT_TAG);

		}
	};
	private Button checkButton;
	private ProgressDialog dialog;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		setContentView(R.layout.parking_view);
		reservation = Reservation.restoreReservation(mContext);
		timerText = (TextView) findViewById(R.id.timer);
		checkButton = (Button) findViewById(R.id.btn_check_in_out);
		if (reservation != null) {
			long remainingTime = reservation.getCheckoutTime().getTime()
					- System.currentTimeMillis();
			counter = new ReservationDounCounter(remainingTime, 1000);
			counter.start();
		} else {
			checkButton.setText(R.string.check_in);
			timerText.setText(R.string.not_checked_in);
		}

	}

	@Override
	protected void onResume() {
		super.onResume();
		mContext.registerReceiver(checkOutReceiver, new IntentFilter(
				CHECK_OUT_TAG));
		if (ConnectionUtils.isConnecting(mContext, CHECK_OUT_TAG)) {
			setSupportProgressBarIndeterminateVisibility(true);
			showLoadingDialog();
		} else if (ConnectionUtils.wasConnected(mContext, CHECK_OUT_TAG))
			if (ConnectionUtils.isResultSuccessful(mContext, CHECK_OUT_TAG))
				checkOutReceiver.onSuccess();
			else
				checkOutReceiver.onFailure();
	}

	@Override
	protected void onPause() {
		super.onPause();
		unregisterReceiver(checkOutReceiver);

		if (dialog != null) {
			dialog.dismiss();
			dialog = null;
		}
		if (!ConnectionUtils.isConnecting(mContext, CHECK_OUT_TAG))
			ConnectionUtils.clearConnection(mContext, CHECK_OUT_TAG);
	}

	public class ReservationDounCounter extends CountDownTimer {
		public ReservationDounCounter(long millisInFuture, long countDownInterval) {
			super(millisInFuture, countDownInterval);
		}

		@Override
		public void onFinish() {
			timerText.setText(R.string.res_time_out);
		}

		@Override
		public void onTick(long millisUntilFinished) {
			timerText.setText(TimeUtils.formatDateFromMillis(millisUntilFinished, "hh:mm:ss"));
			if (millisUntilFinished < 600000)
				timerText.setTextColor(Color.RED);
		}
	}

	public void checkInOutAction(View v) {
		if (reservation != null) {
			showLoadingDialog();
			reservation.checkOut(mContext, CHECK_OUT_TAG);
		} else {
			mContext.startActivity(new Intent(mContext,
					MyReservationsActivity.class));
			finish();
		}
	}

	public void showLoadingDialog() {

		dialog = ProgressDialog.show(this, "",
				getString(R.string.login_please_wait), true, true,
				new OnCancelListener() {

					@Override
					public void onCancel(DialogInterface arg0) {
						setSupportProgressBarIndeterminateVisibility(false);
						Intent loginCancel = new Intent(CHECK_OUT_TAG
								+ HTTPService.CANCEL);
						sendBroadcast(loginCancel);
						ConnectionUtils
								.clearConnection(mContext, CHECK_OUT_TAG);
					}
				});
	}

}